package com.luocc.ccdemo.utils

import android.app.DownloadManager
import android.content.Context
import android.net.Uri
import android.os.Environment
import android.util.Log

object DownloadUtil {

    fun downloadVideo(context: Context, path: String?, allowMobile: Boolean, description: String?) {
        Log.d("LC_download", "downloadVideo: path = $path")
        if (path == null) return
        val index = path.lastIndexOf("/")
        val subPath = "ccdemo" + path.substring(index)
        Log.d("LC_download", "downloadVideo: subPath = $subPath")
        val request = DownloadManager.Request(Uri.parse(path))
        request.setAllowedOverMetered(allowMobile)
            .setAllowedOverRoaming(false)
            .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
            .setTitle(description)
            .setDescription(description)
            .setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS, subPath)
        val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
        downloadManager.enqueue(request)
    }
}